package ade.prime.activity;

import ade.prime.activity.adapter.ResourceFetcher;
import ade.prime.model.AdeWebOptions;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.actionbarsherlock.app.SherlockPreferenceActivity;

public class Preference extends SherlockPreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	this.setTheme(AdeWebOptions.getTheme(this)) ;
        super.onCreate(savedInstanceState) ;

        this.addPreferencesFromResource(R.xml.preferences) ;
        PreferenceManager.getDefaultSharedPreferences(this)
        	.registerOnSharedPreferenceChangeListener(this) ;
    }// onCreate(Bundle

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		String themeKey = ResourceFetcher.getString(
				getBaseContext(), R.string.Preferences_theme) ;
		Log.d("Preference", String.format( //$NON-NLS-1$
				"[onSharedPreferenceChanged] key: '%s' (is theme ? %b)", //$NON-NLS-1$
				key, Boolean.valueOf(key.equals(themeKey)))) ;
		if (key.equals(themeKey)) {
			Log.d("Preference", String.format( //$NON-NLS-1$
					"-- change theme to %d (Dark: %d, Light: %d)", //$NON-NLS-1$
					Integer.valueOf(AdeWebOptions.getTheme(this)),
					Integer.valueOf(R.style.Theme_Sherlock),
					Integer.valueOf(R.style.Theme_Sherlock_Light_DarkActionBar))) ;
	    	this.setTheme(AdeWebOptions.getTheme(this)) ;
		}// if
	}// onSharedPreferenceChanged(SharedPreferences, String)
    
}// class Preference